﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Csla;
using Csla.Data;

namespace Asah.SiteWorx.ProductCatalog
{
    [Serializable()]
    public class Products : BusinessListBase<Products, Product>
    {
        #region Business Methods

        public Product GetProduct(int productId)
        {
            foreach(Product child in this)
            {
                if (child.ID == productId)
                    return child;
            }
            return null;
        }

        public Product GetProduct(Guid uniqueId)
        {
            foreach (Product child in this)
            {
                if (child.UniqueId == uniqueId)
                    return child;
            }
            return null;
        }

        public Product AddNew()
        {
            Product child = Product.NewProduct();
            base.Add(child);
            return child;
        }

        public void Remove(int productId)
        {
            foreach (Product child in this)
            {
                if (child.ID == productId)
                {
                    Remove(child);
                    return;
                }
            }
        }

        public bool Contains(int productId)
        {
            foreach (Product child in this)
            {
                if (child.ID == productId)
                    return true;
            }
            return false;
        }

        public bool ContainsDeleted(int productId)
        {
            foreach (Product child in this)
            {
                if (child.ID == productId)
                    return true;
            }
            return false;
        }
        
        #endregion

        #region Factory Methods

        internal static Products NewProducts()
        {
            return new Products();
        }

        internal static Products GetProducts(SafeDataReader dr)
        {
            return new Products(dr);
        }

        private Products()
        {
            MarkAsChild();
        }

        private Products(SafeDataReader dr)
        {
            Fetch(dr);
            MarkAsChild();
        }
       
        #endregion

        #region Data Access

        private void Fetch(SafeDataReader dr)
        {
            this.RaiseListChangedEvents = false;
            while (dr.Read())
            {
                this.Add(Product.GetProduct(dr));
            }
            this.RaiseListChangedEvents = true;
        }

        internal void Update(Catalog catalog)
        {
            this.RaiseListChangedEvents = false;
            // update (thus deleting) any deleted child objects
            foreach (Product child in DeletedList)
            {
                child.DeleteSelf();
            }
            // now that they are deleted, remove them from memory too
            DeletedList.Clear();

            // add/update any current child objects
            foreach (Product child in this)
            {
                if (child.IsNew)
                    child.Insert(catalog);
                else
                    child.Update(catalog);
            }
            this.RaiseListChangedEvents = true;
        }

        #endregion
    }

   
}
